package cn.xzzz2020.demo.controller;


import cn.xzzz2020.XJynWeb.ioc.core.annotation.Controller;
import cn.xzzz2020.XJynWeb.ioc.inject.annotation.Autowired;
import cn.xzzz2020.XJynWeb.mvc.annotation.RequestMapping;
import cn.xzzz2020.XJynWeb.mvc.annotation.RequestParam;
import cn.xzzz2020.XJynWeb.mvc.annotation.ResponseBody;
import cn.xzzz2020.XJynWeb.mvc.type.ModelAndView;
import cn.xzzz2020.XJynWeb.mvc.type.RequestMethod;
import cn.xzzz2020.demo.service.HeadLineService;
import cn.xzzz2020.demo.entity.bo.HeadLine;
import cn.xzzz2020.demo.entity.dto.Result;

import java.util.List;

@Controller
@RequestMapping(value = "/headline")
public class HeadLineController {
    /**
     * 测试自动注入
     */
    @Autowired
    private HeadLineService headLineService;

    /**
     * 测试MVC框架，执行地址:/headline/add
     * 1.测试请求参数对方法的传参
     * 2.测试对是否支持Post方法
     * 3.测试ModelAndView方式的视图解析器
     *
     * @param lineName 头条标题
     * @param lineLink 头条链接
     * @param lineImg  头条图片
     * @param priority 头条优先级
     * @return 视图数据和视图页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addHeadLine(@RequestParam(value = "lineName", required = true) String lineName,
                                    @RequestParam(value = "lineLink", required = true) String lineLink,
                                    @RequestParam(value = "lineImg", required = true) String lineImg,
                                    @RequestParam(value = "priority", required = true) Integer priority) {
        //添加请求传递过来的参数
        HeadLine headLine = new HeadLine();
        headLine.setLineName(lineName);
        headLine.setLineLink(lineLink);
        headLine.setLineImg(lineImg);
        headLine.setPriority(priority);
        Result<Boolean> result = headLineService.addHeadLine(headLine);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView("addheadline.jsp").addViewData("result", result);
        return modelAndView;
    }

    /**
     * /headline/remove
     * 测试默认解析器
     */
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    public void removeHeadLine() {
        System.out.println("删除headline");
    }

    /**
     * 测试异常解析器
     * 请求路径：/headline/exception
     */
    @RequestMapping(value = "/exception", method = RequestMethod.GET)
    public void throwException() {
        throw new RuntimeException("抛出异常测试");
    }

    /**
     * 测试Json格式的解析
     * 请求路径：/headline/query
     *
     * @return Json结果
     */
    @RequestMapping(value = "query", method = RequestMethod.GET)
    @ResponseBody
    Result<List<HeadLine>> queryHeadLine() {
        //调用IOC注入的Bean
        return headLineService.queryHeadLine(null, 1, 100);
    }


}
